#include "robotcomponent.h"

#include "qdebug.h"

RobotComponent::RobotComponent(ComponentType type, NameGroup names, Dimension3D dimensions,
                               QString description, RobotComponent *parent,
                               QVector3D offset, bool invertLength) :
    QObject(parent),
    _type(type),
    _dimensions(dimensions),
    _offset(offset),
    _inverted(invertLength),
    _description(description)
{
    int i;
    for(i = 0; i < MAX_NAMES; i++){
        _names[i] = names[i];
    }
}

RobotComponent::RobotComponent(ComponentType type, NameGroup names, float radius,
                               QString description, RobotComponent *parent,
                               QVector3D offset, bool invertLength) :
    QObject(parent),
    _type(type),
    _offset(offset),
    _inverted(invertLength),
    _description(description)
{
    int i;
    for(i = 0; i < MAX_NAMES; i++){
        _names[i] = names[i];
    }

    _dimensions.x = 2 * radius;
    _dimensions.y = 2 * radius;
    _dimensions.z = 2 * radius;
}

RobotComponent::~RobotComponent(){
    //Delete the children.
    int i;
    QObjectList children = this->children();
    for(i = 0; i < children.length(); i++){
        delete ((RobotComponent*)children[i]);
    }
}


void RobotComponent::accept(RobotVisitor *visitor){
    int i;
    QObjectList children = this->children();

    //Pre-order traversal
    visitor->pre();
    visitor->visit(this);
    for(i = 0; i < children.length(); i++){
        ((RobotComponent*)children.at(i))->accept(visitor);
    }
    visitor->post();
}


ComponentType RobotComponent::type() const{
    return _type;
}

const unsigned int* RobotComponent::names() const{
    return _names;
}

QVector3D RobotComponent::offset() const{
    return _offset;
}

bool RobotComponent::inverted() const{
    return _inverted;
}

Dimension3D RobotComponent::dimensions() const{
    return _dimensions;
}

QString RobotComponent::description() const{
    return _description;
}

QStack<RobotComponent*> RobotComponent::parentStack() const{
    QStack<RobotComponent*> retStack;

    RobotComponent* curParent = (RobotComponent*)this->parent();
    while(curParent != NULL){
        retStack.push(curParent);
        curParent = (RobotComponent*)curParent->parent();
    }

    return retStack;
}


